$(function () {
    $(".loadingMarsk").css({
        "width":window.screen.width,
        "height":window.screen.height,
    });
    new FastClick(document.body);
    //获取token,orderId,orderSkuId
    var token = localStorage.token;
    var orderId = parseInt(tools.queryString().orderId);
    var orderSkuId = parseInt(tools.queryString().orderSkuId);
    if (!token) {
        location.href = "../loginAndReg/login.html";
        return false;
    }
    function loadOrderDetail(url) {
        $.ajax({
            type: "get",
            url: url,
            beforeSend: function (xhr) {
                $(".loadingMarsk").css("display","block");
                xhr.setRequestHeader("token", token);
            },
            success: function (response) {
                if (response.data.rtnCode === 200) {
                // if (response.rtnCode === 200) {
                    var orderDetail = response.data.data;
                    // var orderDetail = response.data;
                    // orderDetail.address.courier = "[广州市]韵达快递 移到天河区快递点";
                    // orderDetail.address.courierDate = "2017-07-24 22：04：12";
                    $(".orderDetail").html(template("orderDetail", { orderDetail: orderDetail }));
                    $(".loadingMarsk").css("display","none");
                }
                if (response.data.rtnCode === 401) {
                // if (response.rtnCode === 401) {
                    var outTimeCode = response.data.rtnCode;
                    // var outTimeCode = response.rtnCode;
                    location.href = "../../index.html?outTimeCode=" + outTimeCode;
                }
            }
        });
    }
    if (orderId) {
        loadOrderDetail(domain.test + "/getorderdetail?orderId=" + orderId);
        // loadOrderDetail(domain.online + "/order?orderId=" + orderId);
    }
    if (orderSkuId) {
        loadOrderDetail(domain.test + "/getorderdetail?orderSkuId=" + orderSkuId);
        // loadOrderDetail(domain.online + "/order?orderSkuId=" + orderSkuId);
    }
    //取消订单
    $(".orderDetail").on("click", ".cancleOrderBtn", function () {
        $.ajax({
            type: "get",
            url: domain.test + "/cancleorder",
            // url: domain.online + "/order/cancel",
            data: { id: orderId },
            beforeSend: function (xhr) {
                $(".loadingMarsk").css("background-color","rgba(0,0,0,.3)");
                $(".loadingMarsk").css("display","block");
                xhr.setRequestHeader("token", token);
            },
            success: function (response) {
                if (response.data.rtnCode === 200) {
                    // if(response.rtnCode===200) {
                    $(".loadingMarsk").css("display","none");
                    location.reload();
                }
                if (response.data.rtnCode === 401) {
                    // if (response.rtnCode === 401) {
                    var outTimeCode = response.data.rtnCode;
                    // var outTimeCode = response.rtnCode;
                    location.href = "../../index.html?outTimeCode=" + outTimeCode;
                }
            }
        });
    });
    //查看物流
    $(".orderDetail").on("click", ".newlogistics", function () {
        var logisticsPic = $(".productPic").find("img").attr("src");
        localStorage.logisticsPic = logisticsPic;
        location.href = "./logistics.html?skuId=" + orderId;
    });
    //确认收货
    $(".orderDetail").on("click", ".sureCatchOrder", function () {
        var id = orderId;
        $.ajax({
            type: "get",
            url: domain.test + "/surecatchorder",
            // url: domain.online + "/order/sign",
            data: { id: id },
            beforeSend: function (xhr) {
                xhr.setRequestHeader("token", token);
            },
            success: function (response) {
                console.log(response);
                if (response.data.rtnCode === 200) {
                    // if(response.rtnCode===200) {
                    console.log(response);
                }
                if (response.data.rtnCode === 401) {
                    // if (response.rtnCode === 401) {
                    var outTimeCode = response.data.rtnCode;
                    // var outTimeCode = response.rtnCode;
                    location.href = "../../index.html?outTimeCode=" + outTimeCode;
                }
            }
        });
    });
    //商品评价跳转
    $(".orderDetail").on("click", ".commentBtn", function () {
        location.href = "./comment.html?id=" + orderId + "&skuId=" + skuId;
    });
});